<?php
    /**
     * Created by PhpStorm.
     * User: dingjinlai
     * Date: 2019/4/26
     * Time: 上午11:51
     */

    defined('BASEPATH') OR exit('No direct script access allowed');

    /**
     * @property  google_api google_api
     */
    class GoogleApi extends OAuth_Controller
    {

        public function index()
        {
            $this->load->library('google_api');

            $this->load->model('siteweb_model');
            $this->load->model('ga_goods_model');

            $web_list = $this->siteweb_model->siteweb_list();
            $reprot   = array();
            $to_time  = strtotime('today');

            $start_time = !empty($_GET['start_time']) ? strtotime($_GET['start_time']) : strtotime('yesterday');
            $end_time   = !empty($_GET['end_time']) ? strtotime($_GET['end_time']) : strtotime('yesterday');

            foreach ($web_list as $row) {

                if ($row['view_id'] && $row['is_delete'] == 0){
                    print_r($row['web_name']);
                    echo '<br />';
                    $reprot        = $this->google_api->getReport($row['view_id'], $start_time, $end_time);
                    $reprot['web'] = $row;

                    $this->ga_goods_model->insert($reprot);
                }
            }
        }

        public function today_data()
        {
            $this->load->library('google_api');

            $this->load->model('siteweb_model');
            $this->load->model('ga_goods_model');

            $web_list = $this->siteweb_model->siteweb_list();

            $reprot = array();

            $to_time = strtotime('today');

            foreach ($web_list as $row) {
                if ($row['view_id']){
                    $reprot        = $this->google_api->getReport($row['view_id'], strtotime('today'), strtotime('today'));
                    $reprot['web'] = $row;
                }
                //$this->ga_goods_model->insert($reprot);
            }
        }

        public function before_data()
        {
            $this->load->library('google_api');

            $this->load->model('siteweb_model');
            $this->load->model('ga_goods_model');

            $assoc = $this->uri->uri_to_assoc();

            if ($assoc['day'] <= 1){
                exit;
            }

            $web_list = $this->siteweb_model->siteweb_list();

            $reprot = array();

            $to_time = strtotime('today');

            foreach ($web_list as $row) {

                if ($row['view_id']){
                    $reprot        = $this->google_api->getReport($row['view_id'], strtotime("-{$assoc['day']} day"), strtotime("-{$assoc['day']} day"));
                    $reprot['web'] = $row;
                    $this->ga_goods_model->insert($reprot);
                }
            }
        }
    }
